package com.bp;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author andhini
 */
public class Paket {

    public String id_paket;
    public String nama_paket;
//    public double harga;
    public double harga_paket;
    public String lama;
    public String lokasi;
    public String tujuan;
    public String fasilitas;
  private String kuota;

    public Paket() {
    }

    /**
     * @return the id_paket
     */
    public String getId_paket() {
        return id_paket;
    }

    /**
     * @param id_paket the id_paket to set
     */
    public void setId_paket(String id_paket) {
        this.id_paket = id_paket;
    }

    /**
     * @return the nama_paket
     */
    public String getNama_paket() {
        return nama_paket;
    }

    /**
     * @param nama_paket the nama_paket to set
     */
    public void setNama_paket(String nama_paket) {
        this.nama_paket = nama_paket;
    }

    /**
     * @return the harga
     */
//    public double getHarga() {
//        return harga;
//    }
//
//    /**
//     * @param harga the harga to set
//     */
//    public void setHarga(double harga) {
//        this.harga = harga;
//    }
    public double getharga_paket() {
        return harga_paket;
    }

    /**
     * @param harga the harga to set
     */
    public void setharga_paket(double harga_paket) {
        this.harga_paket = harga_paket;
    }

    /**
     * @return the lama_perjalanan
     */
    public String getLama() {
        return lama;
    }

    /**
     * @param lama_perjalanan the lama_perjalanan to set
     */
    public void setLama(String lama) {
        this.lama = lama;
    }

    /**
     * @return the lokasi
     */
    public String getLokasi() {
        return lokasi;
    }

    /**
     * @param lokasi the lokasi to set
     */
    public void setLokasi(String lokasi) {
        this.lokasi = lokasi;
    }

    /**
     * @return the tujuan
     */
    public String getTujuan() {
        return tujuan;
    }

    /**
     * @param tujuan the tujuan to set
     */
    public void setTujuan(String tujuan) {
        this.tujuan = tujuan;
    }

    /**
     * @return the fasilitas
     */
    public String getFasilitas() {
        return fasilitas;
    }

    /**
     * @param fasilitas the fasilitas to set
     */
    public void setFasilitas(String fasilitas) {
        this.fasilitas = fasilitas;
    }

    public void insertDataPaket() {
        try {
            Connection conn = new DbBean_1().getKoneksi();
            Statement s = conn.createStatement();

            String sql = "insert into PAKET values('" + id_paket + "', '" + nama_paket + "'," + harga_paket + ",'" + lama + "','" + lokasi + "','" + tujuan + "','" + fasilitas + "')";
	   //String sql = "insert into PAKET values('" + id_paket + "', '" + nama_paket + "','" + harga_paket + "','" + lama + "','" + lokasi + "','" + tujuan + "','" + fasilitas + "','" +kuota+"')";
	    ResultSet rs = s.executeQuery(sql);
            JOptionPane.showMessageDialog(null, "Selamat data paket berhasil disimpan");
            rs.close();
            s.close();
            conn.close();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Maaf data paket gagal disimpan");
            Logger.getLogger(Paket.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public final String getId_Paket() {
        try {
            String sql = "select max(ID_PAKET) from PAKET";
            Connection con = new DbBean_1().getKoneksi();
            java.sql.Statement sta = con.createStatement();
            ResultSet rs = sta.executeQuery(sql);

            rs.next();
            String result = rs.getString(1);
            if (result != null) {
                int no = Integer.parseInt(result);
                no++;
                sql = "" + no;
                while (sql.length() <= 3) {
                    sql = "0" + sql;
                }
            } else {
                sql = "0001";
            }
            return sql;
        } catch (SQLException ex) {
            Logger.getLogger(Paket.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    public ArrayList lihatDataPaket() {
        ArrayList pakets = new ArrayList();
        try {
            Connection connection = new DbBean_1().getKoneksi();
            Statement s = connection.createStatement();
            String sql = "SELECT * from Paket order by ID_PAKET";
            ResultSet rs = s.executeQuery(sql);
            while (rs.next()) {
                Paket pkt = new Paket();
                pkt.id_paket = rs.getString(1);
                pkt.nama_paket = rs.getString(2);
                pkt.harga_paket =Double.parseDouble(rs.getString(3));
                pkt.lama = rs.getString(4);
                pkt.lokasi = rs.getString(5);
                pkt.tujuan = rs.getString(6);
                pkt.fasilitas = rs.getString(7);
                pkt.kuota = rs.getString(8);
                pakets.add(pkt);
            }
            rs.close();
            s.close();
            connection.close();
        } catch (SQLException e) {
        }
        return pakets;
    }
    
    public Boolean updatePaket(String Id_paket) {
        int result = 0;
        try {
            
             Connection conn = new DbBean_1().getKoneksi();
            Statement s = conn.createStatement();
             
            String sql = "UPDATE PAKET SET HARGA_PAKET=" + harga_paket + ",LAMA='" + lama + "',LOKASI='" + lokasi+ "',FASILITAS='" + fasilitas +"',TUJUAN='" + tujuan +  "' WHERE ID_PAKET='" + Id_paket + "'";
            s.executeUpdate(sql);
            ResultSet rs = s.executeQuery(sql);
            JOptionPane.showMessageDialog(null, "Data Biro berhasil diupdate");
            rs.close();
            s.close();
            conn.close();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,"Data Biro Gagal Diupdate /n" +ex.getMessage());
            Logger.getLogger(Paket.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @return the kuota
     */
    public String getKuota() {
        return kuota;
    }

    /**
     * @param kuota the kuota to set
     */
    public void setKuota(String kuota) {
        this.kuota = kuota;
    }
}
